<?php
  session_start(); // start up your PHP session!
  // create short variable names
  $delay = $_POST['delay'];
  $count = $_POST['count'];
  $uname = $_SESSION['uname'];
  $upass = $_SESSION['upass'];
?>
<html>
<head>
  <title>PetCare</title>
</head>
<body>
<h1>PetCare Parameter Update</h1>
<?php
$fp = fopen('user.cfg', 'r');
	if($fp)
	{    
		$auth = 0;
		while(!feof($fp))
		{
			$name = stream_get_line( $fp, 1024, "\r\n" );
			$pass = stream_get_line( $fp, 1024, "\r\n" );
			if($uname==$name && $upass==$pass)
			{
				$auth = 1;
			}
		}
		fclose($fp);

		if($auth == 1)
		{
			$fp = fopen('user.cfg', 'r');

			$delay = (int)$delay;
			$count = (int)$count;
			$ok = 1;
			if($delay < 1 || $count < 1)
			{
        $_SESSION['message'] = "Delay and Count must be at least 1.";
        header('Location: ChangeParameters.php');     	  
			}
			else
			{
				$filesize = $count*57;
				$fp = fopen('param.cfg', 'w');
				if($fp)
				{
	  				fwrite($fp, "".$delay);
					fwrite($fp, "\n");
					fwrite($fp, "".$count);
					fwrite($fp, "\n");
					fclose($fp);
					
					$_SESSION['delay'] = $delay;
					$_SESSION['count'] = $count;
					
					$_SESSION['message'] = "Parameters are saved successfully.";
					header('Location: ChangeParameters.php');     	  
				}
				else
				{
          $_SESSION['message'] = "Cannot access the server.";
          header('Location: ChangeParameters.php');     	  
				}
			}	
		}
		else
		{
      $_SESSION['message'] = "Authentication failed.";
      header('Location: ChangeParameters.php');     	  
		}
	}
	else
	{
    $_SESSION['message'] = "Cannot access the server.";
    header('Location: ChangeParameters.php');     	  	
	}


?>
</body>
</html>